package xing2387.filtervideoplayer.filter;

import android.opengl.GLES20;
import android.util.Log;

import xing2387.filtervideoplayer.player.gl.FramebufferObject;

public class TestFilter extends GlFilter {
    private FramebufferObject framebufferObject = new FramebufferObject();
    private InnerFilter innerFilter = new InnerFilter();

    @Override
    public void setFrameSize(int width, int height) {
        super.setFrameSize(width, height);
        framebufferObject.setup(width / 2, height / 2);
        innerFilter.setFrameSize(width, height);
    }

    @Override
    public void setVideoSize(int width, int height) {
        super.setVideoSize(width, height);
        innerFilter.setVideoSize(width, height);
    }

    @Override
    public void setup() {
        super.setup();
        innerFilter.setup();
    }

    @Override
    public void release() {
        super.release();
        innerFilter.release();
    }

    @Override
    public void draw(int texName, FramebufferObject fbo) {
        framebufferObject.enable();
        innerFilter.draw(texName, fbo);
        fbo.enable();
        super.draw(framebufferObject.getTexName(), fbo);
    }

    private static class InnerFilter extends GlFilter {

        private static final String VERTEX_SHADER =
                "attribute vec4 aPosition;" +
                        "attribute vec4 aTextureCoord;\n" +
                        "varying highp vec2 vTextureCoord;\n" +
                        "void main() {" +
                        "gl_Position = aPosition;" +
                        "vTextureCoord = vec2(aTextureCoord.x*2.0, aTextureCoord.y*2.0);" +
                        "}";

        protected static final String FRAGMENT_SHADER =
                "precision mediump float;\n" +
                        "varying highp vec2 vTextureCoord;\n" +
                        "uniform lowp sampler2D sTexture;\n" +
                        "void main() {\n" +
                        "gl_FragColor = texture2D(sTexture, vTextureCoord);\n" +
                        "}\n";

        public InnerFilter() {
            super(VERTEX_SHADER, FRAGMENT_SHADER);
        }
    }
}

